const service = require('./publishDetailsUpload.js')
const utilService = require('../../../utils/utilService.js');
// pages/addProduct/shareproducts/shareProduct.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sharedData: {},
    tags: ["一口果冻", "竞拍"],
    activeIndex: '0',
    saleTypeId: 1, // 售卖方式
    leibie: "选择", // 物品分类
    leibieId: '0',
    common: true, // 普通快递
    shunFen: false, // 顺丰快递
    kuaiDiFlag: '0', //快递标识
    disSelectedColor: '#DFDFDF',
    disSelectTextColor: 'gray',
    advanced: false,
    advancedImg: '../../../images/common/packUp.png',
    pickerArray: ['立即上架', '1小时后上架', '2小时后上架', '3小时后上架', '4小时后上架', '5小时后上架'],
    pickerIndex: '0', // 几小时后上架
    priceStr: '50', // 一口果冻价格
    sameCity: '1', // 仅限同城享友
    sharedFriends: '0', // 仅限分享过的享友领取
    attentionFriends: '0', // 仅限我关注的人可以私信给我
    friendsNoSee:'0', // 好友不可见
    putawayNum: '1', // 上架数量
    sliderCount: '1', // 竞拍时间
    auctionPrice: '50', // 起拍价
    currentPrice: '50',
    goodsId: 0, // 商品id
    countAble:false, // 数量是否可以编辑
    shareMsg:"",
    remindStr:"感谢您分享了宝贝，小果冻会尽快审核哒！", // 发布成功文案提示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getShareMessage(8)
    this.setData({
      sharedData: JSON.parse(decodeURIComponent(options.sharedData))
    })
  },

  tabClick: function (e) {
    var value = e.currentTarget.id == 0 ? this.data.priceStr : this.data.auctionPrice
    if (e.currentTarget.id == 0) {
      this.setData({
        activeIndex: 0,
        saleTypeId: 1,// 普通转售
        currentPrice: value,
        countAble:false,
        putawayNum:1
      })
    } else {
      this.setData({
        activeIndex: 1,
        saleTypeId: 3,// 竞拍
        currentPrice: value,
        countAble: true,
        putawayNum: 1

      })
 
    }

  },
  //获取分享信息
  getShareMessage: function (type) {
    var self = this;
    utilService.shareSettings(type,
      function (data) {
        console.log("分享数据", data)
        self.setData({
          shareMsg: data.title
        })
      },
      function (error) {

      }
    );
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var title = this.data.shareMsg
    var path = "pages/home/home?userId=" + getApp().globalData.userId
    return {
      title: title,
      path: path,
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  },
  leibieClick: function (e) {
    wx.navigateTo({
      url: '../classifys/classify?typeID=0' + '&leibieId=' + this.data.leibieId,
    })
  },
  switchChange: function (e) {
    if (e.currentTarget.dataset.types == '1') {
      if (e.detail.value) {
        this.setData({ sameCity: 0 });
      } else {
        this.setData({ sameCity: 1 });
      }
    } else if (e.currentTarget.dataset.types == '2') {
      if (e.detail.value) {
        this.setData({ sharedFriends: 1 });
      } else {
        this.setData({ sharedFriends: 0 });
      }
    } else if (e.currentTarget.dataset.types == '3') {
      if (e.detail.value) {
        this.setData({ attentionFriends: 1 });
      } else {
        this.setData({ attentionFriends: 0 });
      }
    } else if (e.currentTarget.dataset.types == '4') {
      
      if (e.detail.value) {
        this.setData({ friendsNoSee: 1 });
      } else {
        this.setData({ friendsNoSee: 0 });
      }
    }

  },
  commonClick: function (e) {
    this.setData({ common: true, shunFen: false, kuaiDiFlag: 0 });

    console.log('普通快递')
  },
  shunFengClick: function (e) {
    this.setData({ common: false, shunFen: true, kuaiDiFlag: 1 })
    console.log('顺丰快递')
  },
  flowerClick: function (e) {
    this.setData({ selectedBGColor: '#1CBD58', disSelectedColor: '#DFDFDF', sliderHiden: true })
  },
  auctionClick: function (e) {
    this.setData({ selectedBGColor: '#DFDFDF', disSelectedColor: '#1CBD58', sliderHiden: false })
  },
  bottomImgClick: function (e) {
    this.setData({ advanced: !this.data.advanced, scrollTop: 200 })
    if (this.data.advancedImg == '../../../images/common/unfold.png') {
      this.setData({ advancedImg: '../../../images/common/packUp.png' })
    } else {
      this.setData({ advancedImg: '../../../images/common/unfold.png' })
    }
  },
  sliderClick: function (e) {
    this.setData({ sliderCount: e.detail.value })
    console.log(e.detail.value)
  },
  showTost: function (str,icon) {
    wx.showToast({
      title: str,
      icon: icon,
      duration: 1000,
      mask: true
    })
  },
  configAddress: function() {
    var that = this
  },

  /**
   * 点击确认发布
   */
  affirmPublisRequest: function (e) {
    if (this.data.putawayNum < 1){
      this.showTost('商品数不能为0', 'none');
      return
    }
    if (this.data.leibie == '选择'){
      this.showTost('请选择商品分类', 'none');
      return
    }
    if (this.data.currentPrice == " " || this.data.currentPrice == "" || this.data.currentPrice < 0){
      this.showTost('输入果冻数有误', 'none');
  
        return
    }
    let files = '';
    let that = this
    var picPathArray = that.data.sharedData.picArray
    if (that.data.sharedData.isUploadedVideo){// 把视频放到最后
      var videoStr = that.data.sharedData.picArray[0]
      picPathArray.splice(0, 1);
      picPathArray.push(videoStr)
    }
    getApp().func.UploadFile(picPathArray, 0, 0, 0, function (data) {
      //上传成功返回url
      console.log(data)
      that.configAddress()
      files = data;
      let form = new Object();
      form['name'] = that.data.sharedData.titleStr
      form['description'] = that.data.sharedData.describeStr
      form['brandNew'] = that.data.sharedData.isNew
      form['address'] = that.data.sharedData.addressData.id
      form['saleTypeId'] = that.data.saleTypeId
      form['integral'] = that.data.currentPrice
      form['goodsTypeId'] = that.data.leibieId
      form['seeInCity'] = that.data.sameCity
      form['trackType'] = that.data.kuaiDiFlag
      form['count'] = that.data.putawayNum
      form['receiveType'] = that.data.sharedFriends
      form['privateMessageType'] = that.data.attentionFriends
      form['friendsNoSee'] = that.data.friendsNoSee
      form['auctionTime'] = that.data.sliderCount
      form['auctionPrice'] = that.data.auctionPrice
      form['files'] = files

      // 新增发布者的地址详细、用户电话、用户昵称
      form['addressDesc'] = that.data.sharedData.addressData['address']
      form['phone'] = that.data.sharedData.addressData['phone']
      form['userName'] = that.data.sharedData.addressData['name']
      // 新增重量
      form['weight'] = that.data.sharedData.weight
      
      console.log(JSON.stringify(form))
      getApp().func.Post(true, getApp().api.goods_put_data, form,
        function (data) {
          console.log('发布成功')
          that.setData({
            goodsId: data.id
          })
          if (data.successRemind != ""){
            that.setData({
              remindStr: data.successRemind
            })
          }
          wx.reLaunch({
            url: '../messageBack/messageBac?goodsData=' + JSON.stringify(that.data),
          })
        });

    },
      function () {
        console.log('bbbb');
      });

  },
  affirmPublish: function (e) {
    // 获取表单id
    let formId = e.detail.formId;
    getApp().saveUserFormId(formId,17);
      console.log("formId-17",formId)
  },

  sendProduct: function(e){
    var content = '请确认发布宝贝 ' + this.data.sharedData.titleStr
    var that = this
    wx.showModal({
      title: '提示',
      content: content,
      success: function (res) {
        if (res.confirm) {
          that.affirmPublisRequest()
        }
      }
    })
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      pickerIndex: e.detail.value
    })
  },
  priceInput: function (e) {
    this.setData({ priceStr: e.detail.value, currentPrice: e.detail.value })
  },
  putawayNum: function (e) {
    this.setData({ putawayNum: e.detail.value })
  },
  auctionPriceInput: function (e) {
    this.setData({ auctionPrice: e.detail.value, currentPrice: e.detail.value })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

})